#! /usr/bin/python
# -*- coding: cp1250 -*-

import re
import string
import sys
import os
import time
import pepe.p_txt.bigtxt as bt
import pepe.glib as gl
import pepe.p_dm.sdb as sdb

class Dama:
    ''' DAta MAnipulation high-level lib
    
        GOAL
        - automatize most used operations in data manipulation
        - those are : import/export data
                      sha1 check sums
    
        DESIGN PRINCIPLES
        - separatelly usable utils are preferred, and stored in pepe/utils
        - database layer separated (easily interchangeable)

        GENERAL SETUP
        - set_database() - backend database used
            list-of-lists(default)
            shelve
            sqlite
            postgresql

        DATA I/O
        - set_import_txt_separator() - tab defult
        - set_export_txt_separator() - tab defult
        - get_txt() - from text file (tab delimited default)
        - put_txt() - to text file (tab delimited default)
        - get_pgs() - to postgresql file

        UTILS
        - txt_xcut() - cut out on x axis (columns) by interval
        - txt_ycut() - cut out on y axis (rows) by interval
        - hash_file()
        - hash_text()
    '''
    def __init__(self):
        ''' '''
        pass
    
    def get_txt(self, afile, store='list-of-lists'):
        ''' load txt file
            - store options: LOL list-of-lists, ROW per row, PGS PostgreSQL, SQT SQLite
        '''
        if store in ('LOL', 'ROW'):
            res = []; cdel = ''
            for line in bt.lines(afile):
                line = re.sub('\n', '', line)
                if store == 'LOL':
                    if cdel == '': cdel = gl.findcoldelimiter(line)
                    if line <> '': res.append(line.split(cdel))
                if store == 'ROW':
                    if line <> '': res.append(line)
            return res
        else:
            return False

    def put_txt(self, afile, column_delimiter='\t'):
        ''' load txt file
            - store options: LOL list-of-lists, ROW per row, PGS PostgreSQL, SQT SQLite
        '''
        if store in ('LOL', 'ROW'):
            res = []; cdel = ''
            for line in bt.lines(infile):
                line = re.sub('\n', '', line)
                if store == 'LOL':
                    if cdel == '': cdel = gl.findcoldelimiter(line)
                    if line <> '': res.append(line.split(cdel))
                if store == 'ROW':
                    if line <> '': res.append(line)
            return res
        else:
            return False

if __name__ == "__main__":
    ''' '''
    d = Dama()
    data = d.load('example_dama.dat')
    x = gl.lol_column(data, 0)

    sdb.sh_set(key='col1', data=x)
    print sdb.sh_get(key='col1')
    sdb.sh_list()
    sdb.sh_drop('col1')
